package home.conta.bancodedados;

import home.conta.calendar.MyCalendar;
import home.conta.divida.Divida;
import home.conta.list.DividasList;

import java.util.List;

import android.content.Context;
import android.database.Cursor;

public class DividasBD {
	DBAdapter2 db;

	public DividasBD(Context context) {
		db = new DBAdapter2(context);
	}

	public DividasBD(DBAdapter2 db) {
		this.db = db;
	}

	public long insertDivida(Divida d) {
		long id = -1;
		try {
			db.open();
			String nome = d.getNome();
			String info = d.getValor() + "\n" + d.getDataVencimento();
			String infoPagamento = d.getDataPagamento().toString();
			String obs = d.getObservacao();
			id = db.insertDivida( nome, info, infoPagamento, obs);
			db.close();
			d.setId(id);
		} catch (Exception err) {
			err.printStackTrace();
		}
		return id;
	}

	public boolean deleteDivida(Divida d) {
		try{
			db.open();
			boolean deleted = db.deleteDivida(d.getId());
			db.close();
			return deleted;
		} catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}

	public Divida getDivida(long id) {
		try {
			db.open();
			Cursor mCursor = db.getDivida(id);
			if (mCursor != null) {
				
				int index = mCursor.getColumnIndex(DBAdapter2.KEY_ROWID);
				long idDivida = mCursor.getLong(index);

				index = mCursor.getColumnIndex(DBAdapter2.KEY_ISBN);
				String nome = mCursor.getString(index);

				index = mCursor.getColumnIndex(DBAdapter2.KEY_TITLE);
				String info[] = mCursor.getString(index).split("\n");
				//System.out.println(info);
				double valor = Double.parseDouble(info[0]);
				MyCalendar dataV = new MyCalendar(MyCalendar.formataData(info[1]));
				
				index = mCursor.getColumnIndex(DBAdapter2.KEY_PUBLISHER);
				String infoPagamento = mCursor.getString(index);
				MyCalendar dataP = new MyCalendar(MyCalendar.formataData(infoPagamento));
				
				index = mCursor.getColumnIndex(DBAdapter2.KEY_OBSERVATION);
				String obs = mCursor.getString(index);
				
				db.close();
				return new Divida(idDivida, nome, valor, dataV, dataP, obs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Divida> getAllDividas(){
		DividasList dividasList = new DividasList();
		
		try{
			db.open();
			Cursor mCursor = db.getAllDividas();
			if (mCursor.moveToFirst()) {
			    do {
	
			    	int index = mCursor.getColumnIndex(DBAdapter2.KEY_ROWID);
					long idDivida = mCursor.getLong(index);

					index = mCursor.getColumnIndex(DBAdapter2.KEY_ISBN);
					String nome = mCursor.getString(index);
					//System.out.println(nome);

					index = mCursor.getColumnIndex(DBAdapter2.KEY_TITLE);
					String info[] = mCursor.getString(index).split("\n");
					//System.out.println(info[0] + " -- " + info[1]);
					double valor = Double.parseDouble(info[0]);
					MyCalendar dataV = new MyCalendar(MyCalendar.formataData(info[1]));
					
					index = mCursor.getColumnIndex(DBAdapter2.KEY_PUBLISHER);
					String infoPagamento = mCursor.getString(index);
					MyCalendar dataP = new MyCalendar(MyCalendar.formataData(infoPagamento));
					
					index = mCursor.getColumnIndex(DBAdapter2.KEY_OBSERVATION);
					String obs = mCursor.getString(index);
					
					dividasList.add(new Divida(idDivida, nome, valor, dataV, dataP, obs ));
					
			    } while (mCursor.moveToNext());
			}
			db.close();
			return dividasList.getNaoPagas();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean updateDivida(long id, Divida d)
	{
		try{
			db.open();
			String nome = d.getNome();
			String info = d.getValor() + "\n" + d.getDataVencimento();
			String infoPagamento = d.getDataPagamento().toString();
			String obs = d.getObservacao();
			boolean updated = db.updateDivida(id, nome, info, infoPagamento, obs);
			db.close();
			
			return updated;
		} catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
